public class IsBalanceTree {
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if(pRoot == null){
            return true;
        }
        if(Math.abs(getHigh(pRoot.left) - getHigh(pRoot.right)) > 1){
            return false;
        }
        return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
    }

    private int getHigh(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = getHigh(root.left) + 1;
        int right = getHigh(root.right) + 1;
        return Math.max(left,right);
    }
}


